package domain.components;

import images.GameImagesMap;
import images.Images;

import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.GameComponent;
import com.uqbar.vainilla.appearances.Appearance;
import com.uqbar.vainilla.events.constants.Key;

import domain.scenes.MainGameScene;
import domain.worm.Worm;

public class SightComponent extends GameComponent<MainGameScene> {
	
	private int diameter;

	private double angulo = 90;
	
	private boolean puedeRotar = false;
	 
	private Worm worm;
	
	public SightComponent(double x, double y, Worm worm){
		super((x+worm.getDiameter()/2)+(worm.getDir()*worm.getDiameter())-25,y+(worm.getDiameter()/2)-25);
		this.worm = worm;
		this.angulo *= worm.getDir();
		this.diameter = 5;
		this.setAppearance(this.getImageSight());
	}
	
	private Appearance getImageSight() {
		return GameImagesMap.getImage("sight");
	}

	public int getDiameter(){
		return this.diameter;
	}
	
	public double getAngulo() {
		return angulo;
	}
	
	public void update(DeltaState deltaState) {
		
		
		if(deltaState.isKeyBeingHold(Key.UP) | deltaState.isKeyPressed(Key.UP)){
				if(this.worm.getDir() ==1 && this.angulo < 180){
					this.puedeRotar = true;
				}else{if(this.angulo < 0){
						this.puedeRotar = true;
					}
				}
				if(this.puedeRotar){
					double anguloRads = Math.toRadians(this.angulo);
					double mvx = deltaState.getDelta() * 80 * Math.cos(anguloRads);
					double mvy = deltaState.getDelta() * 80 * Math.sin(anguloRads);
					this.angulo =Math.toDegrees(deltaState.getDelta()*3 + anguloRads);
					this.move(mvx, mvy);
					this.puedeRotar = false;
				}
			
		}
		
		if(deltaState.isKeyBeingHold(Key.DOWN) | deltaState.isKeyPressed(Key.DOWN)){
			if(this.worm.getDir() ==-1 && this.angulo > -180){
				this.puedeRotar = true;
			}else{if(this.angulo > 0){
					this.puedeRotar = true;
				}
			}
			if(this.puedeRotar){
				double anguloRads = Math.toRadians(this.angulo);
				double mvx = -(deltaState.getDelta() * 80 * Math.cos(anguloRads));
				double mvy = -(deltaState.getDelta() * 80 * Math.sin(anguloRads));
				this.angulo = Math.toDegrees(anguloRads - deltaState.getDelta()*3);
				this.move(mvx, mvy);
				this.puedeRotar = false;
			}
		}
		

}
	
}
